package com.hhsjdesign.dynamicui.utils

import android.content.Context
import android.util.Log
import com.hhsjdesign.dynamicui.core.widgetdata.WidgetData
import com.hhsjdesign.dynamicui.core.widgetdata.WidgetTypeConst
import com.hhsjdesign.dynamicui.core.widgetimpl.IWidget
import java.lang.reflect.Constructor

object WidgetFactory {

    //真正的创建Widget生命周期对象
    @JvmStatic
    fun createWidget(context: Context, widgetData: WidgetData): IWidget? {
        val clazz: Class<out IWidget>? = ClassCachePool.getClassByWidgetType(
            ClassCachePool.WIDGET,
            if (widgetData.type.isNullOrEmpty()) WidgetTypeConst.LAYOUT_LINEAR_VERTICAL else widgetData.type!!
        ) as? Class<out IWidget>
        val constructor: Constructor<out IWidget>? =
            clazz?.getDeclaredConstructor(Context::class.java, WidgetData::class.java)
        return constructor?.newInstance(context, widgetData)?.also {
            Log.e("hhsjh", "创建组件 ${it.javaClass.simpleName}")
        }

    }
}